using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RUPS
{
    public partial class AdaugareClient : Form
    {
        public AdaugareClient()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool eroare=false;

            toolStripStatusLabel1.ForeColor = Color.Red;
            System.Text.RegularExpressions.Regex expresie=new System.Text.RegularExpressions.Regex(@"\S+@\S+\.\S+");
            if ((textBoxNumeClient.Text == "") || (textBoxPrenumeClienti.Text == "") || (textBoxTelefonClient.Text == "") || (textBoxEmailClient.Text == ""))
            {
                eroare = true;
                errorProvider1.SetError(groupBox1, "Acestea sunt date obligatorii!");
                
            }
            else errorProvider1.SetError(groupBox1,"");
            if (textBoxTelefonClient.Text.Length < 10 && !eroare)
            {
                eroare = true;
                errorProvider1.SetError(textBoxTelefonClient, "Numarul de telefon nu este complet! (sunt asteptate 10 cifre)");
                
            }
            else errorProvider1.SetError(textBoxTelefonClient,"");

            if (!expresie.IsMatch(textBoxEmailClient.Text) && !eroare)
            {
                eroare = true;
                errorProvider1.SetError(textBoxEmailClient, "Acesta nu este o adresa de mail valida!");
                
            }
            else errorProvider1.SetError(textBoxEmailClient,"");
            //else if (textBoxTelefonClient.Text.
            //else
            //    errorProvider1.SetError(groupBox1, "");

            if (!eroare)
            {
                CreareClientTableAdapters.clientiTableAdapter tac = new RUPS.CreareClientTableAdapters.clientiTableAdapter();
                //CreareClient.clientiDataTable dt = tac.GetClienti();
                //CreareClient.clientiRow dr = dt.NewclientiRow();
                //dr.nume = textBoxNumeClient.Text;
                //dr.prenume = textBoxPrenumeClienti.Text;
                //dr.email = textBoxEmailClient.Text;
                //dr.telefon = textBoxTelefonClient.Text;
                //dr.adresa1 = textBoxAdresa1Client.Text;
                //dr.adresa2 = textBoxAdresa2Client.Text;
                //dr.localitate = textBoxLocalitateClient.Text;
                //dr.tip = checkBoxPersoanaJuridicaClient.Checked;
                //dt.AddclientiRow(dr);
                //tac.Update(dr);
                try
                {
                    tac.InsertClient(textBoxNumeClient.Text, textBoxPrenumeClienti.Text, textBoxAdresa1Client.Text, textBoxAdresa2Client.Text, textBoxLocalitateClient.Text, textBoxEmailClient.Text, textBoxTelefonClient.Text, checkBoxPersoanaJuridicaClient.Checked);
                    toolStripStatusLabel1.ForeColor = Color.Black;
                    toolStripStatusLabel1.Text = "Clientul a fost introdus!";
                    textBoxNumeClient.Text = "";
                    textBoxPrenumeClienti.Text = "";
                    textBoxTelefonClient.Text = "";
                    textBoxEmailClient.Text = "";
                    textBoxAdresa1Client.Text = "";
                    textBoxAdresa2Client.Text = "";
                    textBoxLocalitateClient.Text = "";
                    checkBoxPersoanaJuridicaClient.Checked = false;
                }
                catch (Exception)
                {
                    //label8.ForeColor = Color.Red;
                    toolStripStatusLabel1.Text = "Eroare: Nu s-a reusit introducerea!";
                }

            }
            else
            {
                //label8.ForeColor = Color.Red;
                toolStripStatusLabel1.Text = "Eroare! Verificati semnul de exclamare.";
                    
            } 
        }

        private void textBoxTelefonClient_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == '.')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}